//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.spring.schedule_system.dengxiaojiao.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;

public class LyBeanUtils {
    private static final Map<LyBeanUtils.CopierIdentity, BeanCopier> MAP_CACHES = new HashMap();
    private static Lock initLock = new ReentrantLock();

    public LyBeanUtils() {
    }



    public static <T, R> R copyProperties(T source, R target, BiConsumer<T, R> action, Converter c) {
        if (null != source && null != target) {
            getBeanCopier(source.getClass(), target.getClass(), c).copy(source, target, c);
            if (action != null) {
                action.accept(source, target);
            }

            return target;
        } else {
            return null;
        }
    }

    private static LyBeanUtils.CopierIdentity generateKey(Class<?> class1, Class<?> class2, boolean userConvert) {
        return new LyBeanUtils.CopierIdentity(class1, class2, userConvert);
    }

    private static BeanCopier getBeanCopier(Class<?> class1, Class<?> class2, Converter c) {
        LyBeanUtils.CopierIdentity baseKey = generateKey(class1, class2, null != c);
        BeanCopier copier = (BeanCopier)MAP_CACHES.get(baseKey);
        if (null == copier) {
            try {
                initLock.lock();
                copier = (BeanCopier)MAP_CACHES.get(baseKey);
                if (null == copier) {
                    copier = BeanCopier.create(class1, class2, null != c);
                    MAP_CACHES.put(baseKey, copier);
                }
            } finally {
                initLock.unlock();
            }
        }

        return copier;
    }


    private static class CopierIdentity {
        private Class<?> source;
        private Class<?> target;
        private boolean useConvert;

        private CopierIdentity() {
        }

        private CopierIdentity(Class<?> source, Class<?> target, boolean useConvert) {
            this.source = source;
            this.target = target;
            this.useConvert = useConvert;
        }

        public boolean isUseConvert() {
            return this.useConvert;
        }

        public void setUseConvert(boolean useConvert) {
            this.useConvert = useConvert;
        }

        public Class<?> getSource() {
            return this.source;
        }

        public void setSource(Class<?> source) {
            this.source = source;
        }

        public Class<?> getTarget() {
            return this.target;
        }

        public void setTarget(Class<?> target) {
            this.target = target;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            } else if (o != null && this.getClass() == o.getClass()) {
                LyBeanUtils.CopierIdentity copierIdentity = (LyBeanUtils.CopierIdentity)o;
                return this.source == copierIdentity.source && this.target == copierIdentity.target && this.useConvert == copierIdentity.useConvert;
            } else {
                return false;
            }
        }

        public int hashCode() {
            return Objects.hash(new Object[]{this.source, this.target, this.useConvert});
        }
    }
}
